/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.sunspotworld.ConexionBD;
import org.sunspotworld.HidroponiaHost;

/**
 *
 * @author Salaver
 */
public class Tiempos extends javax.swing.JFrame {

    ConexionBD con = new ConexionBD();
    public int i = 0;
    /**
     * Creates new form Tiempos
     */
    public Tiempos() {
        initComponents();
        i=1;
        cargar();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jSpinner1 = new javax.swing.JSpinner();
        jSpinner2 = new javax.swing.JSpinner();
        jSpinner3 = new javax.swing.JSpinner();
        jSpinner4 = new javax.swing.JSpinner();
        jSpinner5 = new javax.swing.JSpinner();
        jSpinner6 = new javax.swing.JSpinner();
        jSpinner7 = new javax.swing.JSpinner();
        jSpinner8 = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jTable1.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Rango", "Alto", "Bajo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);

        jSpinner1.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner1StateChanged(evt);
            }
        });

        jSpinner2.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jSpinner2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner2StateChanged(evt);
            }
        });

        jSpinner3.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jSpinner3.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner3StateChanged(evt);
            }
        });

        jSpinner4.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jSpinner4.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner4StateChanged(evt);
            }
        });

        jSpinner5.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jSpinner5.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner5StateChanged(evt);
            }
        });

        jSpinner6.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jSpinner6.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner6StateChanged(evt);
            }
        });

        jSpinner7.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jSpinner7.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner7StateChanged(evt);
            }
        });

        jSpinner8.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jSpinner8.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner8StateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel1.setText("Duracion de Riego (minutos)");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel2.setText("Duracion de Ciclo (minutos)");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel3.setText("Duracion de Etapa (dias)");

        jLabel4.setFont(new java.awt.Font("Tahoma", 2, 16)); // NOI18N
        jLabel4.setText("Mezcla NFT:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 2, 16)); // NOI18N
        jLabel5.setText("Semillero:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 2, 16)); // NOI18N
        jLabel6.setText("Mezcla NFT:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 2, 16)); // NOI18N
        jLabel7.setText("Semillero:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 2, 16)); // NOI18N
        jLabel8.setText("Semillero:");

        jLabel9.setFont(new java.awt.Font("Tahoma", 2, 16)); // NOI18N
        jLabel9.setText("Etapa #1:");

        jLabel10.setFont(new java.awt.Font("Tahoma", 2, 16)); // NOI18N
        jLabel10.setText("Etapa #2:");

        jLabel11.setFont(new java.awt.Font("Tahoma", 2, 16)); // NOI18N
        jLabel11.setText("Etapa #3:");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        jButton1.setText("Actualizar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        jButton2.setText("Cerrar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel13.setIcon(new javax.swing.ImageIcon("C:\\Users\\Salaver\\Documents\\NetBeansProjects\\host\\trunk\\HidroponiaDesktop\\img\\rsz_1358373535_free-clock-face-vector-black.png")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addGap(89, 89, 89)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jSpinner3, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jSpinner4, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel3)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel10)
                                    .addComponent(jLabel11))
                                .addGap(89, 89, 89)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jSpinner7, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jSpinner8, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jSpinner5, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jSpinner6, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(23, 23, 23))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(31, 31, 31)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jSpinner3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jSpinner4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addGap(23, 23, 23)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jSpinner5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jSpinner6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel13)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jSpinner7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jSpinner8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addGap(41, 41, 41)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addGap(12, 12, 12))))
        );

        jPanel2.setBackground(new java.awt.Color(204, 204, 204));
        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel12.setIcon(new javax.swing.ImageIcon("C:\\Users\\Salaver\\Documents\\NetBeansProjects\\host\\trunk\\HidroponiaDesktop\\img\\horariostitulo.png")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel12)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 491, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
 
    private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner1StateChanged
        if (i==0){
        try {
            // TODO add your handling code here:
            con.conectar();
            if (Integer.parseInt(jSpinner1.getValue().toString()) > 0){
                String query1 = "UPDATE etapa SET duracion="+Integer.parseInt(jSpinner1.getValue().toString())+" WHERE id=1";
                con.insertar(query1);
                HidroponiaHost.cosecha.etapa.actualizarTiempos();
            } else{
            //custom title, error icon
                JOptionPane.showMessageDialog(this,
                "No puede realizar esta operacion.",
                "Error",
                JOptionPane.ERROR_MESSAGE);
            }
            con.desconectar();
        
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_jSpinner1StateChanged

    private void jSpinner2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner2StateChanged
        // TODO add your handling code here:
        if (i==0){
        try {
            // TODO add your handling code here:
            con.conectar();
            if (Integer.parseInt(jSpinner2.getValue().toString()) > 0){
                String query1 = "UPDATE etapa SET duracion="+Integer.parseInt(jSpinner2.getValue().toString())+" WHERE id=2";
                String query2 = "UPDATE etapa SET duracion="+Integer.parseInt(jSpinner2.getValue().toString())+" WHERE id=3";
                String query3 = "UPDATE etapa SET duracion="+Integer.parseInt(jSpinner2.getValue().toString())+" WHERE id=4";
                con.insertar(query1);
                con.insertar(query2);
                con.insertar(query3);
                HidroponiaHost.cosecha.etapa.actualizarTiempos();
            } else{
            //custom title, error icon
                JOptionPane.showMessageDialog(this,
                "No puede realizar esta operacion.",
                "Error",
                JOptionPane.ERROR_MESSAGE);
            }
            con.desconectar();
        
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_jSpinner2StateChanged

    private void jSpinner3StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner3StateChanged
        // TODO add your handling code here:
        if (i==0){
        try {
            // TODO add your handling code here:
            con.conectar();
            if ((Integer.parseInt(jSpinner3.getValue().toString()) > 0) && (Integer.parseInt(jSpinner1.getValue().toString()) < Integer.parseInt(jSpinner3.getValue().toString()))){
                String query1 = "UPDATE etapa SET ciclo="+Integer.parseInt(jSpinner3.getValue().toString())+" WHERE id=1";
                con.insertar(query1);
                HidroponiaHost.cosecha.etapa.actualizarTiempos();
            } else{
            //custom title, error icon
                JOptionPane.showMessageDialog(this,
                "No puede realizar esta operacion.",
                "Error",
                JOptionPane.ERROR_MESSAGE);
            }
            con.desconectar();
        
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_jSpinner3StateChanged

    private void jSpinner5StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner5StateChanged
        // TODO add your handling code here:
        if (i==0){
        try {
            // TODO add your handling code here:
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            con.conectar();
            if (Integer.parseInt(jSpinner5.getValue().toString()) > 0){
                String query1 = "UPDATE etapa SET fase="+Integer.parseInt(jSpinner5.getValue().toString())+" WHERE id=1";
                con.insertar(query1);
                HidroponiaHost.cosecha.etapa.actualizarTiempos();
            } else{
                //custom title, error icon
                    JOptionPane.showMessageDialog(this,
                    "No puede realizar esta operacion.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
                }
            con.desconectar();
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_jSpinner5StateChanged

    private void jSpinner6StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner6StateChanged
        // TODO add your handling code here:
        if (i==0){
        try {
            // TODO add your handling code here:
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            con.conectar();
            if (Integer.parseInt(jSpinner6.getValue().toString()) > 0){
                String query1 = "UPDATE etapa SET fase="+Integer.parseInt(jSpinner6.getValue().toString())+" WHERE id=2";
                con.insertar(query1);
                HidroponiaHost.cosecha.etapa.actualizarTiempos();
            } else{
                //custom title, error icon
                    JOptionPane.showMessageDialog(this,
                    "No puede realizar esta operacion.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
                }
            con.desconectar();
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        } /*catch (ParseException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        }
    }//GEN-LAST:event_jSpinner6StateChanged

    private void jSpinner7StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner7StateChanged
        // TODO add your handling code here:
        if (i==0){
        try {
            // TODO add your handling code here:
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            con.conectar();
            if (HidroponiaHost.cosecha.etapa.getId() == 3){
                String query2 = "SELECT fecha_inicio FROM etapa_cosecha WHERE etapa_id=3 AND cosecha_id="+HidroponiaHost.cosecha.getId();
                ResultSet rs2 = con.consultar(query2);
                rs2.last();
                if (rs2.getRow()>0){
                rs2.beforeFirst();
                rs2.next();
                if ((Integer.parseInt(jSpinner7.getValue().toString()) > 0) && ((sdf.parse(rs2.getString(1)).getTime()+HidroponiaHost.dia*Integer.parseInt(jSpinner7.getValue().toString()))>now.getTime())){
                        String query1 = "UPDATE etapa SET fase="+Integer.parseInt(jSpinner7.getValue().toString())+" WHERE id=3";
                        con.insertar(query1);
                        HidroponiaHost.cosecha.etapa.actualizarTiempos();
                } else{
                //custom title, error icon
                    JOptionPane.showMessageDialog(this,
                    "No puede realizar esta operacion.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
                }
                }
                
            } else if (Integer.parseInt(jSpinner7.getValue().toString()) > 0){
                String query1 = "UPDATE etapa SET fase="+Integer.parseInt(jSpinner7.getValue().toString())+" WHERE id=3";
                con.insertar(query1);
                HidroponiaHost.cosecha.etapa.actualizarTiempos();
            }
            con.desconectar();
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_jSpinner7StateChanged

    private void jSpinner8StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner8StateChanged
        // TODO add your handling code here:
        if (i==0){
        try {
            // TODO add your handling code here:
            System.out.println((Integer.parseInt(jSpinner8.getValue().toString()) > 0)+"spinner "+Integer.parseInt(jSpinner8.getValue().toString()));
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            con.conectar();
            if (Integer.parseInt(jSpinner8.getValue().toString()) > 0){
                String query1 = "UPDATE etapa SET fase="+Integer.parseInt(jSpinner8.getValue().toString())+" WHERE id=4";
                con.insertar(query1);
                HidroponiaHost.cosecha.etapa.actualizarTiempos();
            } else{
                //custom title, error icon
                    JOptionPane.showMessageDialog(this,
                    "No puede realizar esta operacion.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
                }
            con.desconectar();
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_jSpinner8StateChanged

    private void jSpinner4StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner4StateChanged
        // TODO add your handling code here:
        if (i==0){
        try {
            // TODO add your handling code here:
            con.conectar();
            if ((Integer.parseInt(jSpinner4.getValue().toString()) > 0) && (Integer.parseInt(jSpinner2.getValue().toString()) < Integer.parseInt(jSpinner4.getValue().toString()))){
                String query1 = "UPDATE etapa SET ciclo="+Integer.parseInt(jSpinner4.getValue().toString())+" WHERE id=2";
                String query2 = "UPDATE etapa SET ciclo="+Integer.parseInt(jSpinner4.getValue().toString())+" WHERE id=3";
                String query3 = "UPDATE etapa SET ciclo="+Integer.parseInt(jSpinner4.getValue().toString())+" WHERE id=4";
                con.insertar(query1);
                con.insertar(query2);
                con.insertar(query3);
                HidroponiaHost.cosecha.etapa.actualizarTiempos();
            } else{
            //custom title, error icon
                JOptionPane.showMessageDialog(this,
                "No puede realizar esta operacion.",
                "Error",
                JOptionPane.ERROR_MESSAGE);
            }
            con.desconectar();
        
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_jSpinner4StateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (HidroponiaHost.cosecha.mezcla != null){
        try {
            // TODO add your handling code here:
            HidroponiaHost.cosecha.mezcla.rangoPH.setAlto(Integer.valueOf(jTable1.getValueAt(0, 1).toString()));
            HidroponiaHost.cosecha.mezcla.rangoPH.setBajo(Integer.valueOf(jTable1.getValueAt(1, 1).toString()));
            HidroponiaHost.cosecha.mezcla.rangoC.setAlto(Integer.valueOf(jTable1.getValueAt(0, 2).toString()));
            HidroponiaHost.cosecha.mezcla.rangoC.setBajo(Integer.valueOf(jTable1.getValueAt(1, 2).toString()));
            con.conectar();
            String query1 = "UPDATE rangos SET alto="+Integer.valueOf(jTable1.getValueAt(0, 1).toString())+", bajo="+Integer.valueOf(jTable1.getValueAt(1, 1).toString())+" WHERE id=1";
            String query2 = "UPDATE rangos SET alto="+Integer.valueOf(jTable1.getValueAt(0, 2).toString())+", bajo="+Integer.valueOf(jTable1.getValueAt(1, 2).toString())+" WHERE id=2";
            con.insertar(query1);
            con.insertar(query2);
            con.desconectar();
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }
        } else {
            JOptionPane.showMessageDialog(this,
                "Cosecha aun en etapa semillero, no puede modificar los rangos.",
                "Error",
                JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_formWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    public void cargar(){
        try {
            con.conectar();
            String query = "SELECT id, duracion, ciclo, fase FROM etapa";
            ResultSet rs = con.consultar(query);
            while (rs.next()){
                if (rs.getInt(1)==1){
                    this.jSpinner1.setValue(Integer.parseInt(rs.getString(2)));
                    this.jSpinner3.setValue(Integer.parseInt(rs.getString(3)));
                    this.jSpinner5.setValue(Integer.parseInt(rs.getString(4)));
                } else if (rs.getInt(1)==2){
                    this.jSpinner2.setValue(Integer.parseInt(rs.getString(2)));
                    this.jSpinner4.setValue(Integer.parseInt(rs.getString(3)));
                    this.jSpinner6.setValue(Integer.parseInt(rs.getString(4)));
                } else if (rs.getInt(1)==3){
                    this.jSpinner7.setValue(Integer.parseInt(rs.getString(4)));
                } else if (rs.getInt(1)==4){
                    this.jSpinner8.setValue(Integer.parseInt(rs.getString(4)));
                }
            }
            con.desconectar();
            con.conectar();
            String query2 = "SELECT id, nombre, alto, bajo FROM rangos";
            ResultSet rs2 = con.consultar(query2);
            while (rs2.next()){
                if (rs2.getInt(1) == 1){
                    jTable1.setValueAt(rs2.getString(2), 0, 0);
                    jTable1.setValueAt(rs2.getInt(3), 0, 1);
                    jTable1.setValueAt(rs2.getInt(4), 0, 2);
                } else{
                    jTable1.setValueAt(rs2.getString(2), 1, 0);
                    jTable1.setValueAt(rs2.getInt(3), 1, 1);
                    jTable1.setValueAt(rs2.getInt(4), 1, 2);
                }
            }
            con.desconectar();
            i=0;
        } catch (SQLException ex) {
            Logger.getLogger(Tiempos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Tiempos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Tiempos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Tiempos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Tiempos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tiempos().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    private javax.swing.JSpinner jSpinner3;
    private javax.swing.JSpinner jSpinner4;
    private javax.swing.JSpinner jSpinner5;
    private javax.swing.JSpinner jSpinner6;
    private javax.swing.JSpinner jSpinner7;
    private javax.swing.JSpinner jSpinner8;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
